<?php
/*                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                            O\ = /O
 *                        ____/`---'\____
 *                      .   ' \\| |// `.
 *                       / \\||| : |||// \
 *                     / _||||| -:- |||||- \
 *                       | | \\\ - /// | |
 *                     | \_| ''\---/'' | |
 *                      \ .-\__ `-` ___/-. /
 *                   ___`. .' /--.--\ `. . __
 *                ."" '< `.___\_<|>_/___.' >'"".
 *               | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *                 \ \ `-. \_ __\ /__ _/ .-` / /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *
 *         .............................................
 *                  佛祖保佑             永无BUG
 *
 * ======================================================
 * @author: Ethan Lu <ethan.lu@qq.com>
 * 
 */

namespace frontend\widgets;

use common\models\Attribute;
use yii\base\InvalidParamException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;

class GoodsFilter extends Widget
{

    public $filterItems = [];

    public $attribute;

    public function init()
    {
        array_unshift($this->filterItems, '不限');
        if(!$this->attribute) {
            throw new InvalidParamException('attribute must be configured');
        }

    }

    public function run()
    {
        $request = \Yii::$app->getRequest();
        $params = $request->getQueryParams();

        array_unshift($params, 'index');
        foreach($this->filterItems as $k => $v) {

            if(is_array($v)) {
                if(array_key_exists('value', $v)) {
                    $getValue = $v['value'];
                }
            } else {
                $getValue = $v;
            }

            $params[$this->attribute] = $getValue;
            if($request->getQueryParam($this->attribute) == $getValue) {
                $options = ['class' => 'act'];
            } else {
                $options = [];
            }

            if($v == '不限') {
                unset($params[$this->attribute]);
                if($request->getQueryParam($this->attribute) == null) {
                    $options = ['class' => 'act'];
                }
            }
            if(is_array($v)) {
                if(array_key_exists('label', $v)) {
                    echo Html::a($v['label'], $params, $options);

                }
            } else {
                echo Html::a($v, $params, $options);
            }
        }
    }


}